home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / Dev / Amiga-E / E_v3.2a_extras / PdSrc / E-Empty / empty-handler.e next >
Text File  |  1995-01-29  |  3KB  |  173 lines

  1.  
  2. /***
  3.  
  4.     empty-handler.e v1.00
  5.  
  6.     By Vidar Hokstad <vidarh@rforum.no>
  7.  
  8.     This code is hereby declared public domain.
  9.  
  10.     Returns the number of ASCII NUL (0) specified as filename:
  11.     "Copy Empty:100 Ram:test" will create a file "test" in Ram:
  12.     containing 100 ASCII NUL
  13.  
  14.     The code uses the ReplyPkt() function of V36+ dos.library.
  15.     To use it with 1.2/1.3, uncomment the replypkt() function, and
  16.     change the case of the "ReplyPkt()" calls to "replypkt"
  17.  
  18.     Based on "empty-handler" in C by o.wagner@aworld-2.zer[.sub.org]
  19.  
  20.     Needs AmigaE3.1a or newer
  21.  
  22. ***/
  23.  
  24. OPT OSVERSION=37,PREPROCESS
  25.  
  26. MODULE 'dos/dos','dos/dosextens','dos/filehandler','exec/ports',
  27.         'exec/nodes'
  28.  
  29.  
  30. #define Baddr(val) Shl(val,2)
  31.  
  32. PROC getpacket (p:PTR TO process)
  33.     DEF port:PTR TO mp,
  34.         msg:PTR TO mn
  35.  
  36.     port:= p.msgport        -> The port of our process
  37.     WaitPort (port)            -> Wait for a message
  38.     msg:= GetMsg (port)    
  39. ENDPROC msg.ln::ln.name
  40.  
  41. /*
  42. PROC replypkt (packet:PTR TO dospacket,res1,res2)
  43.  
  44.     DEF msg:PTR TO mn,
  45.         replyport:PTR TO mp,p:PTR TO process
  46.  
  47.     p:= FindTask(0)
  48.  
  49.     ->--- Set return codes
  50.  
  51.     packet.res1:=res1
  52.     packet.res2:=res2
  53.  
  54.     ->--- Find reply port
  55.  
  56.     replyport:=packet.port
  57.  
  58.     ->--- Pointer to the execmessage of the packet
  59.  
  60.     msg:=packet.link
  61.  
  62.     ->--- Set packet-port
  63.  
  64.     packet.port:=p.msgport
  65.  
  66.     ->--- "Connect" message and packet
  67.  
  68.     msg.ln::ln.name:=packet;
  69.     msg.ln::ln.succ:=NIL
  70.     msg.ln::ln.pred:=NIL
  71.  
  72.     ->--- ... and send message
  73.     PutMsg(replyport,msg)
  74. ENDPROC
  75. */
  76.  
  77. PROC main ()
  78.     DEF hproc:PTR TO process,
  79.         packet:PTR TO dospacket,
  80.         devnode:PTR TO devicenode,
  81.         fh:PTR TO filehandle,o:PTR TO mn
  82.  
  83.     DEF running=TRUE,
  84.         opencount=0,
  85.         emptylen,readlen,c,
  86.         nump:PTR TO CHAR,
  87.         filename[64]:STRING,typ
  88.  
  89.     ->--- Initialize handler
  90.  
  91.     hproc:= FindTask(0)
  92.     o:= wbmessage
  93.     packet:=o.ln::ln.name  -> getpacket(hproc)
  94.     wbmessage:=0
  95.  
  96.     devnode:= Baddr (packet.arg3)
  97.     devnode.task:= hproc.msgport
  98.  
  99.     ->--- Return startup packet
  100.  
  101.     ReplyPkt (packet,DOSTRUE,0)
  102.  
  103.     ->--- Main loop
  104.  
  105.     WHILE running
  106.         packet:= getpacket (hproc)
  107.         typ:=packet.type
  108.         SELECT typ
  109.  
  110.             CASE ACTION_FINDINPUT
  111.                 nump:= Baddr(packet.arg3)
  112.                 c:=0
  113.                 WHILE c<nump[]
  114.                     filename[c]:=nump[c+1]
  115.                     INC c
  116.                 ENDWHILE
  117.                 filename[c]:=0
  118.                 nump:= filename
  119.                 WHILE (nump[] AND (nump[]<>":")) DO INC nump
  120.                 IF nump[]=":" THEN INC nump
  121.                 emptylen:=Val (nump)
  122.                 INC opencount
  123.  
  124.                 ->--- Filehandle
  125.  
  126.                 fh:= Baddr(packet.arg1)
  127.                 fh.interactive:=0                 -> Non-interactive file
  128.                 fh.args:=fh
  129.                 fh.arg2:=emptylen
  130.  
  131.                 ->--- Return packet
  132.                 ReplyPkt(packet,DOSTRUE,0)
  133.  
  134.             CASE ACTION_END
  135.                 ->--- If no open files, end the handler
  136.  
  137.                 DEC opencount
  138.                 running:= opencount<>0
  139.                 ReplyPkt(packet,DOSTRUE,0)
  140.  
  141.             CASE ACTION_READ
  142.  
  143.                 ->--- FileHandle from open
  144.                 fh:=packet.arg1
  145.                 nump:=packet.arg2
  146.                 emptylen:=fh.arg2
  147.                 readlen:=packet.arg3
  148.  
  149.                 ->--- Check for "end of file"
  150.                 readlen:=Min(readlen,emptylen)
  151.  
  152.                 c:=0
  153.                 WHILE c<readlen
  154.                     nump[]:=0
  155.                     INC nump
  156.                     INC c
  157.                 ENDWHILE
  158.  
  159.                 ->--- Subtract lenght
  160.                 fh.arg2:=fh.arg2-readlen
  161.  
  162.                 ReplyPkt (packet,readlen,0)
  163.  
  164.  
  165.             CASE ACTION_WRITE
  166.                 ReplyPkt (packet,DOSFALSE,ERROR_DISK_WRITE_PROTECTED)
  167.             DEFAULT
  168.                 ReplyPkt (packet,DOSFALSE,ERROR_ACTION_NOT_KNOWN)
  169.         ENDSELECT
  170.     ENDWHILE
  171.     devnode.task:=FALSE
  172. ENDPROC
  173.